package cn.agiledata.bank.common.dao;



import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class HibernateDaoUtil extends HibernateDaoSupport {
	
	public List getObjectList(String objName){
		List list = (List)this.getHibernateTemplate().find("from "+objName);
		return list;
	}
	
	
	public Object getObjectById(Object obj,Long id){
		return this.getHibernateTemplate().get(obj.getClass(), id);
	}
	
	public Object getObjectById(Class obj,Long id){
		return this.getHibernateTemplate().get(obj, id);
	}
	
	public Object loadObjectById(Object obj,Long id){
		return this.getHibernateTemplate().load(obj.getClass(), id);
	}
	public Object getObjectByHqlPara(final String hql,final Map paraMap){
		List resultList = getObjectListByHqlPara(hql,paraMap);
		if (resultList.size()>0){
			return resultList.get(0);
		}
		return null;
	}
	public List getObjectListByHqlPara(final String hql,final Map paraMap){
		List obj = (List)this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException{
				Query query = session.createQuery(hql);
				Iterator iter = paraMap.keySet().iterator();
				while(iter.hasNext()){
					Object key = iter.next();
					Object entry = paraMap.get(key);
					if (entry instanceof List){
						query.setParameterList((String)key, (List)entry);
					}else{
						query.setParameter((String)key, entry);
					}
				}
				return query.list();
			}
		});
		//getHibernateTemplate().setFlushMode(getHibernateTemplate().FLUSH_AUTO);
		return obj;
	}
	public Integer updateObjectList(final String hql,final Map paraMap){
		Integer obj = (Integer)this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException{
				Query query = session.createQuery(hql);
				Iterator iter = paraMap.keySet().iterator();
				while(iter.hasNext()){
					Object key = iter.next();
					Object entry = paraMap.get(key);
					if (entry instanceof List){
						query.setParameterList((String)key, (List)entry);
					}else{
						query.setParameter((String)key, entry);
					}
				}
				return new Integer(query.executeUpdate());
			}
		});
		//getHibernateTemplate().setFlushMode(getHibernateTemplate().FLUSH_AUTO);
		return obj;		
	}
	public void saveOrUpdateObject(Object obj){
		this.getHibernateTemplate().saveOrUpdate(obj);
	}
	
	public void saveOrUpdateList(List obj){
		this.getHibernateTemplate().saveOrUpdateAll(obj);
	}
	
	public boolean ifExistObj(Object obj,Serializable objId){
		//boolean result = false;
		Object object = 
			this.getHibernateTemplate().get(obj.getClass(), objId);
		if (object != null){
			return true;
		}
		else return false;
	}
	
	public boolean ifExistObj(String HQLstr,Map map){
		List result = getObjectListByHqlPara(HQLstr,map);
		if (result.size()>0)
			return true;
		else return false; 
	}
	
	public boolean ifExistObj(String HQLstr){
		Map map = new HashMap();
		return this.ifExistObj(HQLstr, map);
	}
}
